package com.whcoding.async.controller;

import com.whcoding.async.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: spring-boot-learning
 * @description: 异步控制器
 * @author: whcoding
 * @create: 2022-02-23 16:45
 **/

@RestController
@RequestMapping("/async")
public class AsyncController {

	@Autowired
	AsyncService asyncService;

	/**
	 * http://localhost:9270/async/syncHelloTask
	 * 同步
	 * 页面需要的等待3秒才能返回数据
	 *
	 * @return
	 */
	@GetMapping("/syncHelloTask")
	public String syncHelloTask() {
		asyncService.syncHelloTask();
		return "同步方法：syncHelloTask,3秒后返回结果";
	}

	/**
	 * http://localhost:9270/async/asyncHelloTask
	 * 异步
	 *
	 * @return
	 */
	@GetMapping("/asyncHelloTask")
	public String asyncHelloTask() {
		asyncService.asyncHelloTaskSleep();
		return "异步方法：asyncHelloTask,通过async自动执行,立即执行 " + System.currentTimeMillis();
	}
}
